#ifndef SOFTKEYBOARD_H
#define SOFTKEYBOARD_H

#include <QWidget>
#include <QToolButton>
#include <QLabel>
#include <QLineEdit>
#include <QFile>
#include <QMessageBox>
#include <QStackedWidget>
#include <QMouseEvent>
#include <QPoint>
#include <QMouseEvent>
#include <QDialog>
#include <QShowEvent>
#include <QLayout>
#include <QRect>
#include <QPainter>
#include "mydragablewidget.h"
#include "myhdragabletranslateview.h"
#include "myvdragabletranslateview.h"
#include "myqstringview.h"
#include "server/chinesecharacterserver.h"
#include "entity/characterentity.h"
#include "chineseInput/xydatabaseoperation.h"
#include "chineseInput/xyinputsearchinterface.h"
#include "chineseInput/xytranslateitem.h"


class MYMovableLabel: public QLabel
{
    Q_OBJECT
public:
    MYMovableLabel(QWidget *parent = NULL)
        :QLabel(parent)
    {
        //this->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    }

protected:
    bool event(QEvent *event);
};


class SoftKeyboard : public QWidget
{
    Q_OBJECT

public:
    explicit SoftKeyboard(QWidget *parent = nullptr);
    ~SoftKeyboard();

    void fillCandidateText(QStringList);   //填充候选框文本

    /*主要的对外接口*/
    void selectKeyboardStyle(int num);//选择键盘样式
    void setMoveEnabled(bool moveEnabled=true);//设置无边框窗口移动使能
    void showInputBufferArea(QString inputContent=QString());//显示输入缓存区域
    void hideInputBufferArea(QLineEdit *currLineEdit);//隐藏输入缓存区域

    /* 给子布局的接口 */

    void addCandidateCharacterText(QString character);  //非中文输入时的添加字符到输入框 / 中文输入时将选中的候选框的文字添加到候选框
    void switchPage(int type);      //切换键盘

    void clear_history();           //重置候选框

    void search_begin(QList<XYTranslateItem *> lists);  //根据候选字母映射出候选词

    void userSelectChinese(const QString&, int);    //拼音输入时点击候选词的处理方法
    bool a2zkey_clicked(int unicode, int key);      //添加中文搜索字母

    QLineEdit *textShow;        //文本显示区域


private:

    MyDragableWidget* getHDragableWidget();
    MyHDragableTranslateView* gettranslateHView();
    QString alreadyInputLetters; //用户输入的所有字母
    QStringList alreadySelectTranslates; //用户选择的所有中文词组

    MyDragableWidget            *translateHDragableWidget;  //水平候选词拖拽控件
    MyDragableWidget            *translateVDragableWidget;  //垂直候选词拖拽控件

    MYMovableLabel              *letterLabel;               //输入字母显示控件，同时提供对键盘的移动操作

    QRect                       triangleBtnRect;            //记录三角按钮的矩形框
    bool                        triangleBtnPressed;         //记录三角按钮是否按下

    /* 相关配置参数  */
    int winSizeH = 800;   //键盘顶层布局高度，默认(最大)800
    int winSizeW = 1000;   //键盘顶层布局宽度，默认(最大)1300
    int candidateWordNum = 8;  //动态变化的候选词数量
    int kSizeH = 300, kSizeW = 800;     //栈布局的高与宽
    int tSizeW = 100;                    //选项框的宽
    float a;    //根据界面大小等比例变换窗口大小

    int previous = 1; //上一次展示的键盘类型

    ChineseCharacterServer chineseServer;

    /* 控件相关对象 */
    QStackedWidget *sKey;           //放置键盘的栈布局
    QVBoxLayout *globalVLayout;
    QLabel *keyTypeTab[4];          //更改键盘的控件
    QWidget *keyTypePage[5];        //键盘

    QRect applicationRect;      //显示屏相关数据对象


    //无边框窗口移动相关参数
    QPoint cursorGlobalPos;
    bool isMousePress=true;
    bool isMoveEnabled;

    /* 控件相关初始化方法 */
    void init();            //初始化函数
    void initKeyboard();    //初始化键盘
    void initCandidate();   //初始化候选框

    //键盘隐藏动画
    void moveToHide();

    //键盘出现动画
    void moveToShow();

    bool moveable = true;

    void clearCandidate();  //清空候选框


signals:
    void triangleBtnClicked();
    void send_commit(const QString&);

public slots:
    void triangleBtnClickedOP();



protected:
    /* 重写的相关函数 */
    void showEvent(QShowEvent *event) override;         //重写窗口绘制函数
    bool eventFilter(QObject *obj, QEvent *event) override;

    //通过这三个事件处理函数实现无边框窗口的移动
    void mousePressEvent(QMouseEvent *event) override;
    void mouseMoveEvent(QMouseEvent *event) override;
    void mouseReleaseEvent(QMouseEvent *event) override;
    void paintEvent(QPaintEvent *e);



public slots:
//    void candidateLetterChangedSlot(QString text);//候选字母改变响应槽
//    void candidateWordBtnSlot();//候选词被点击的响应槽

    /* 特殊按键响应槽 */
    void deleteSlot();//删除输入的响应槽
    void enterSlot();//回车被按下的响应槽
    void spaceSlot(); //空格被按下的响应槽
    void finishSlot();  //完成输入键被点击的响应槽
    void getTextByHand(CharacterEntity character);

    friend class EN_KeyBoard;

};

#endif // SOFTKEYBOARD_H
